<?php
require_once "DB.php";

$host = "localhost";
$user = "root";
$passwd = "gw1stlst";
$db_name = "restaurant_mgmt";

$dsn = "mysql://$user:$passwd@$host/$db_name";

$db =& DB::connect($dsn);
//$db->setFetchMode(DB_FETCHMODE_ASSOC);
$db->setFetchMode(DB_FETCHMODE_OBJECT);
//$db->fetchmode=3;
$db->getAll("set NAMES utf8");

if(PEAR::isError($db)){
	die($db->getMessage());
}

function gQuery($sql){
	global $db;
	$data =& $db->getAll($sql);
	//print_r($db);
	if(PEAR::isError($data)){
		die($data->getMessage());
	}else{
		return $data;
	}
}

/**
 * 执行带数据的操作，如INSERT/UPDATE
 * 
 * @param String $sql SQL语句，如"INSERT INTO tabl_name (c1,c2,c3) VALUES(?,!,&);"
 * @param Mixed $data 组成数组的数据
 * @return Boolean 
 */
function gExecute($sql, $data){
	global $db;
	$sth = $db->prepare($sql);
	if(PEAR::isError($sth)){
		die($sth->getMessage());
		return false;
	}

	$re =& $db->execute($sth, $data);
	if(PEAR::isError($re)){
		die($re->getMessage());
		return false;
	}
	return true;
}

?>
